# -*- coding:utf8 -*-

import json
import time
import configparser
import threading
import queue

from channels import Group
# from django.core.management import BaseCommand
from socketTwo.conf import all_conf
from socketTwo.tools.zabbix_tigger import zabbix_tg

# option支持大小写
class myconf(configparser.ConfigParser):
    def optionxform(self, optionstr):
        return optionstr

# class Command(BaseCommand):
#
#     def handle(self, *args, **options):
#         # rc = redis.Redis(host=settings.REDIS_OPTIONS["HOST"],
#         #                  port=settings.REDIS_OPTIONS["PORT"],
#         #                  password=settings.REDIS_OPTIONS["PASSWORD"],
#         #                  db=settings.REDIS_OPTIONS["DB"])
#         # rc.delete(all_conf.GROUP_NAME)
#         start_code = threading_data()
#         start_code.threading_run()

class threading_data(object):
    def __init__(self):
        self.kafka_lag_data=[]
        self.zabbix_data = []
        self.zcell_num_data = {}
        self.zabbix_queue = queue.LifoQueue(30)
        self.zcell_num_queue = queue.LifoQueue(30)
        self.all_data = {}

    def zabbix_t(self):
        while True:
            trigger = zabbix_tg()
            self.zabbix_queue.put(trigger.trigger_data())
            print("trigger success")
            time.sleep(30)

    def process_data(self):
        while True:
            if not self.zabbix_queue.empty():
                self.zabbix_data = self.zabbix_queue.get()
                Group(all_conf.GROUP_NAME).send({'text': json.dumps(self.zabbix_data)})
                # con.hset(all_conf.GROUP_NAME, 'zcell', json.dumps(self.zabbix_data))
            time.sleep(5)

    def threading_run(self):
        t1 = threading.Thread(target=self.zabbix_t)
        # t5 = threading.Thread(target=self.process_data, args=(rediscon,))
        t5 = threading.Thread(target=self.process_data)
        t1.start()
        t5.start()


